from xkw_UI_auto.BasePages.basePage import BasePage
from xkw_UI_auto.LocatorEelements.bk_locator.home_locator import HomeLocator as loc
from selenium.webdriver.common.by import By


js = 'let manager = document.querySelector("downloads-manager").shadowRoot;let ironList = manager.querySelector(' \
     '"iron-list");let downloadItem = ironList.querySelector("downloads-item").shadowRoot;var ' \
     'input=downloadItem.querySelector("#title-area #file-link").innerText;return input '


class HomePage(BasePage):

    # 退出按钮是否存在
    def logout_button_isExist(self):
        doc = '备课_首页_退出按钮'
        return self.wait_element_presence(loc.logout_button, doc=doc)

    # 提交文本意见反馈
    def submit_feed_back(self, pic, text=''):
        doc = '备课_首页_意见反馈'
        self.click_element(loc.feed_back, doc=doc)
        self.input_text(loc.input_desc, text, doc=doc)
        self.upload_file_notInput(loc.feed_back_pic, pic, doc)
        self.click_element(loc.submit_btn, doc=doc)

    # 文件上传
    def upload_file(self, file, fileName):
        doc = '备课_首页_上传文件'
        self.upload_file_notInput(loc.upload_btn, file, doc)
        self.input_text(loc.input_file_name, fileName, doc)
        self.click_element(loc.submit_file, doc)

    # 查找文件是否存在
    def file_reName_isExist(self, name):
        doc = '备课_首页_查找文件名'
        return self.wait_element_presence((By.XPATH, '//span[@class="span-title" and text()="{}"]'.format(name)),
                                          doc=doc)

    # 查找文件是否存在
    def file_isExist(self):
        doc = '备课_首页_查找文件名'
        return self.wait_element_presence(loc.get_file, doc=doc)

    # 获取页面中间提示信息
    def get_page_centerMsg(self):
        doc = '备课_首页_页面中间提示信息'
        return self.get_element_text(loc.center_Msg, doc=doc)

    # 删除上传文件、删除文件夹
    def delete_file(self, op_type='file'):
        doc = '备课_首页_删除文件'
        self.click_element(loc.drop_btn, doc)
        attribute = self.get_element_attribute(loc.drop_btn, 'aria-describedby', doc)
        self.click_element((By.XPATH, "//div[@id='{}']//li[@title='删除']".format(attribute)), doc)
        if op_type == 'file':
            self.click_element(loc.certain_del_btn, doc)
        else:
            self.click_element(loc.del_folder_submit, doc)
        self.refresh_page()

    # 文件重命名、文件夹重命名
    def update_fileName(self, reName):
        doc = '备课_首页_修改文件名'
        self.click_element(loc.drop_btn, doc)
        attribute = self.get_element_attribute(loc.drop_btn, 'aria-describedby', doc)
        self.click_element((By.XPATH, "//div[@id='{}']//li[@title='重命名']".format(attribute)), doc)
        self.input_text(loc.reName_fileName, reName, doc)
        self.click_element(loc.reName_fileName_submit, doc)

    # 移动文件
    def remove_file(self):
        doc = '备课_首页_移动文件'
        self.click_element(loc.drop_btn, doc)
        attribute = self.get_element_attribute(loc.drop_btn, 'aria-describedby', doc)
        self.click_element((By.XPATH, "//div[@id='{}']//li[@title='移动到']".format(attribute)), doc)
        self.click_element(loc.move_file_locator, doc)
        self.click_element(loc.move_file_submit, doc)
        self.refresh_page()

    # 文件下载
    def download_file(self):
        doc = '备课_首页_下载文件'
        self.click_element(loc.download_btn, doc)

    # 获取下载的文件名
    def get_download_fileName(self):
        doc = '备课_首页_获取下载文件名'
        self.switch_new_windows_handle(doc)
        text = self.exec_js(js, doc)
        self.back_default_page()
        return text

    # 新增教材
    def new_textbook(self):
        doc = '备课_首页_新增教材'
        self.click_element(loc.exchange_tb, doc)
        self.click_element(loc.click_newBtn_tb, doc)
        self.click_element(loc.school_section_tb, doc)
        self.click_element(loc.subject_tb, doc)
        self.click_element(loc.version_tb, doc)
        self.click_element(loc.textbook_tb, doc)
        self.click_element(loc.textbook_tb_submit, doc)

    # 获取备课中心当前教材名称
    def get_bk_textbook_name(self):
        doc = '备课_首页_获取当前教材名称'
        return self.get_element_text(loc.textbook_name, doc=doc)

    # 判断添加教材界面是否存在某教材
    def add_textbook_name_isExists(self):
        doc = '备课_首页_获取添加教材界面的新增教材名称'
        self.click_element(loc.exchange_tb, doc)
        flags = self.wait_element_presence(loc.change_tb, doc=doc)
        self.click_element(loc.exchange_tb, doc)
        return flags

    # 切换教材
    def change_textbook(self):
        doc = '备课_首页_切换教材'
        self.click_element(loc.exchange_tb, doc)
        self.click_element(loc.change_tb, doc)

    # 默认教材
    def default_textbook(self):
        doc = '备课_首页_切换到默认教材'
        self.click_element(loc.exchange_tb, doc)
        self.click_element(loc.default_tb, doc)

    # 删除教材
    def delete_textbook(self):
        doc = '备课_首页_删除教材'
        self.click_element(loc.exchange_tb, doc)
        self.perform_action_chains(loc.change_tb, doc)
        self.click_element(loc.del_textbook_del_btn, doc)
        self.click_element(loc.del_textbook_submit_btn, doc)

    # 新建文件夹
    def new_folder(self, folderName):
        doc = '备课中心_首页_新建文件夹'
        self.click_element(loc.new_folder, doc=doc)
        self.input_text(loc.reName_fileName, folderName, doc)
        self.click_element(loc.reName_fileName_submit, doc)

    # 回到默认页面
    def back_default_page(self):
        doc = '备课中心_首页'
        self.keep_default_windows_handle('e备课 - 首页 - 轻松备好课', doc)

    # 客户端下载按钮
    def home_client_download(self):
        doc = '备课中心_首页_客户端下载'
        self.click_element(loc.client_download_btn, doc)

    # 备课客户端下载界面标题、文案
    def get_client_download_page_context(self):
        doc = '备课中心_首页_客户端下载页面'
        self.switch_new_windows_handle(doc)
        title, text = self.get_page_title(doc), self.get_element_text(loc.client_download_page_desc, doc)
        self.back_default_page()
        return title, text

    # 帮助中心
    def help_page(self):
        doc = '备课中心_首页_帮助中心'
        self.perform_action_chains(loc.user_img, doc)
        attribute = self.get_element_attribute(loc.user_get_id, 'aria-describedby', doc)
        help_loc = (By.CSS_SELECTOR, '#{} span.bk-head-options a'.format(attribute))
        self.click_element(help_loc, doc)

    # 帮助中心校验
    def get_help_context(self):
        doc = '备课中心_首页_帮助中心页面'
        self.switch_new_windows_handle(doc)
        title, flags = self.get_page_title(doc), self.wait_element_presence(loc.bk_help_page, doc=doc)
        self.back_default_page()
        return title, flags

    # 导航栏的找资源
    def get_nav_bar_source(self):
        doc = '备课中心_首页_导航栏找资源'
        self.click_element(loc.nav_source, doc)
